/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistance.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author pwitek
 */
@Entity
@Table(name = "users")
public class User implements Serializable {

    public boolean isInRole(String role) {
        boolean result = false;
        for (UserRole r : this.getRoles()){
            if (r.getRoleName().equals(role)){
                result = true;
                break;
            }
        }
        return result;
    }
    public enum State { INACTIVE, ACTIVE, BANNED }

    private static final long serialVersionUID = 1L;
    
    @Id
    private String username;
    @Column
    private String email;
    @Column
    private String name;
    @Column
    private String surname;
    @Column
    private String password;
    @Column
    private State userState;
    @Column //TODO: dodać coś takiego, że ma security role najwyższą ze wszystkich ról w rolach
    private String securityRole;
    
    @OneToMany(mappedBy = "user")
    private List<UserRole> roles = new ArrayList<UserRole>();

    public String getSecurityRole() {
        return securityRole;
    }

    public void setSecurityRole(String securityRole) {
        this.securityRole = securityRole;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public List<UserRole> getRoles() {
        return roles;
    }

    public String getSurname() {
        return surname;
    }

    public State getUserState() {
        return userState;
    }

    public String getUsername() {
        return username;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public void setUserState(State userState) {
        this.userState = userState;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.User[id=" + username + "]";
    }

}
